package cn.edu.tju.elm.repository;

import cn.edu.tju.elm.model.Order;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Optional;

/**
 * ClassName: OrderRepository
 * Package: cn.edu.tju.elm.repository
 * Description:
 *
 * @Author Niu Wenhang
 * @Create 2025/9/17 22:19
 * @Version 1.0
 */
public interface OrderRepository extends JpaRepository<Order, Long> {
    List<Order> findByCustomerIdAndDeletedFalse(Long customerId);
    
    @Query("select o from Order o left join fetch o.orderDetailets d left join fetch d.food where o.id = :id")
    Optional<Order> findOrderWithDetailsAndFoodById(@Param("id") Long id);
    // 根据店铺ID查询订单
    List<Order> findByBusinessIdAndDeletedFalse(Long businessId);
    
    // 根据用户ID和订单状态查询
    List<Order> findByCustomerIdAndOrderStateAndDeletedFalse(Long customerId, Integer orderState);
    
    // 根据用户ID和日期范围查询
    List<Order> findByCustomerIdAndOrderDateBetweenAndDeletedFalse(Long customerId, LocalDate startDate, LocalDate endDate);
    
    // 根据用户ID和金额范围查询
    List<Order> findByCustomerIdAndOrderTotalBetweenAndDeletedFalse(Long customerId, BigDecimal minTotal, BigDecimal maxTotal);
    
    // 查询最近的订单（限制数量）
    List<Order> findTopByCustomerIdAndDeletedFalseOrderByCreateTimeDesc(Long customerId, Pageable pageable);
}
